﻿
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
using System.Web.Security;
using SmallShop.Frame;

namespace SmallShop.BaseWeb
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class SmallController: Controller
    {
        
        #region User
        
        private UserInfo _userInfo;
        
        /// <summary>
        /// 当前用户
        /// </summary>
        /// <returns></returns>
        protected UserInfo CurrentUser()
        {
            if (_userInfo == null )
            {
                var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (cookie != null && cookie.Value != null)
                {
                    var ticket = FormsAuthentication.Decrypt(cookie.Value);
                    if (ticket != null)
                        _userInfo = ticket.UserData.Deserialize<UserInfo>();
                }
                if (_userInfo == null)
                {
                    _userInfo = new UserInfo();
                }
            }
            return _userInfo;
        }

        /// <summary>
        /// 设置当前用户
        /// </summary>
        /// <param name="userInfo"></param>
        protected void SetCurrentUser(UserInfo userInfo)
        {
            _userInfo = userInfo;
        }

        #endregion

        #region json

        protected new JsonetResult Json(object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet)
        {
            return Json(data, "application/json", Encoding.UTF8, behavior);
        }

        protected new JsonetResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonetResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }

        #endregion json
    }
}
